﻿Imports System.Data.OleDb

Public Class 登陆窗体

#Region "按扭外观"

    '退出按扭
    Private Sub PictureBox2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox2.MouseLeave
        ButtonPic(PictureBox2, "Exit_1.jpg")
    End Sub
    Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
        ButtonPic(PictureBox2, "Exit_2.jpg")
    End Sub

    '登入按扭
    Private Sub PictureBox1_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox1.MouseLeave
        ButtonPic(PictureBox1, "confirm_1.jpg")
    End Sub
    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        ButtonPic(PictureBox1, "confirm_2.jpg")
    End Sub

#End Region

    '快捷键
    Private Sub 登陆窗体_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        If e.KeyCode = Keys.Enter Then
            PictureBox1_Click(sender, e)
        End If
    End Sub

    '窗体加载
    Private Sub 登陆窗体_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        '连接数据库
        cnn = getAccessConnection(New_dbConnectionString)

        '读设置
        readSet()

        '读旧数据库相关设置
        ReadSettings()

    End Sub

    '读设置
    Private Sub readSet()
        Dim str As String = "select * from 设置表"
        Dim dataSet_s As DataSet = SQLtoDataSet(str)
        Dim remem As Boolean = dataSet_s.Tables(0).Rows(0).Item(1)
        If remem Then
            CheckBox1.Checked = True
            TextBox1.Text = dataSet_s.Tables(0).Rows(0).Item(2)
            TextBox2.Focus()
        Else
            TextBox1.Text = ""
        End If

    End Sub

    '登陆按扭
    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

        PictureBox1.Visible = False
        PictureBox2.Visible = False
        PictureBox3.Visible = True

        AccountName = TextBox1.Text
        Dim sqlStr As String = "select * from 用户表 where 用户名 = '" + AccountName + "'"

        '搜索
        Dim dataSet As DataSet = SQLtoDataSet(sqlStr)

        If dataSet.Tables(0).Rows.Count > 0 Then
            '用户存在
            Dim adminpw As String = dataSet.Tables(0).Rows(0).Item("密码").ToString
            'MsgBox("用户名：" + AccountName + vbCrLf + "密码：" + adminpw)

            '验证密码
            If TextBox2.Text = adminpw Then
                PictureBox3.Visible = False
                'MsgBox("密码正确")

                '用户权限
                WriteRight = dataSet.Tables(0).Rows(0).Item("修改权限").ToString    '修改权限
                ReadRight = dataSet.Tables(0).Rows(0).Item("查看权限").ToString     '查看权限
                ManageRight = dataSet.Tables(0).Rows(0).Item("户管权限").ToString   '管理权限

                'MsgBox("修改权限:" + WriteRight.ToString + vbCrLf + "查看权限:" + ReadRight.ToString + vbCrLf + "管理权限:" + ManageRight.ToString)

                主父窗体.Show()
                Me.Hide()

            Else
                PictureBox3.Visible = False
                MsgBox("密码不正确")
                TextBox2.Focus()
            End If
        Else
            '用户不存在
            PictureBox3.Visible = False
            MsgBox("用户名’" + AccountName + "‘不存在")
            TextBox1.Focus()
        End If

        dataSet.Dispose()

        PictureBox1.Visible = True
        PictureBox2.Visible = True
        PictureBox3.Visible = False

        CheckBox1_CheckedChanged(sender, e)

    End Sub

    '退出按扭
    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click

        cnn.Close()

        '关闭本程序所有线程，释放内存
        End
        Application.Exit()

    End Sub

    '写入设置
    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.Click
        Dim setting1 As Boolean = CheckBox1.Checked
        Dim setting2 As String = TextBox1.Text

        Dim sqlstr As String = "update 设置表 set 记录用户名 = " + setting1.ToString + ",已记用户名 = '" + setting2 + "'"
        '执行
        SQLupdata(sqlstr)

    End Sub

End Class
